package com.xwj.back.controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.xwj.back.pojo.Store;
import com.xwj.back.service.StoreService;
import com.xwj.back.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StoreController {

    @Autowired
    private StoreService storeService;

    @GetMapping("/store/get/")
    public JSONObject getStores(@RequestParam Map<String,String> data){
        Integer page = Integer.parseInt(data.get("page"));
        String role = data.get("role");
        String username = data.get("username");
        return storeService.getStores(username,role,page);
    }

    @GetMapping("/store/getAll/")
    public List<Store> getStoreAll(){
        return storeService.getStoreAll();
    }

    @PostMapping("/store/add/")
    public Map<String,String> addStore(@RequestBody Map<String,String> data){
        Map<String, String> map = new HashMap<>();
        String result = storeService.addStore(data);
        map.put("message",result);
        return map;
    }

    @PostMapping("/store/update/")
    public Map<String,String> updateStore(@RequestParam Map<String,String> data){
        Map<String,String> map = new HashMap<>();
        String result = storeService.updateStore(data);
        map.put("message",result);
        return map;
    }

    @PostMapping("/store/delete/")
    public Map<String,String> deleteStore(@RequestBody Map<String,String> data){
        Map<String,String> map = new HashMap<>();
        String result = storeService.deleteStore(data);
        map.put("message",result);
        return map;
    }

    //批量删除
    @PostMapping("/store/deleteSelect/")
    public Map<String,String> deleteSelect(@RequestBody Map<String,String> data){
        Map<String,String> map = new HashMap<>();
        String result = storeService.deleteSelect(data);
        map.put("message",result);
        return map;
    }
    //导出全部数据
    @GetMapping("/store/exportAll/")
    public void exportExcel(HttpServletResponse response) throws IOException {
        storeService.ExportExcel(response);
    }
    //选择导出
    @PostMapping("/store/export/")
    public void batchExport(@RequestBody List<Integer> data, HttpServletResponse response) throws IOException {
        storeService.batchExport(data,response);
    }
    //导入数据
    @PostMapping("/store/import/excel/")
    public Map<String,String> importExcel(MultipartFile file) throws IOException {
        return storeService.ImportExcel(file);
    }
    //根据选择的类型查询
    @PostMapping("/store/get/byType/")
    public List<Store> getStoreByType(@RequestBody Map<String,String> data){
        String username = data.get("username");
        String role = data.get("role");
        List<String> types = JSONObject.parseObject(data.get("types"),new TypeReference<List<String>>(){});
        return storeService.getStoreByType(types,username,role);
    }

    //模糊查询
    @GetMapping("/store/get/Like")
    public List<Store> getStoreByLike(@RequestParam("username") String username,@RequestParam("role") String role,
                     @RequestParam("storeName") String storeName,@RequestParam("storeBoss") String storeBoss){
        return storeService.getStoreByLike(username,role,storeName,storeBoss);
    }


    //上传商标
    @PostMapping("/store/upload/logo/")
    public Map<String,String> uploadLogoImg(MultipartFile file) throws IOException {
        String upload = FileUploadUtil.upload(file);
        Map<String, String> map = new HashMap<>();
        map.put("logoUrl",upload);
        return map;
    }
}
